package base.controller;

import base.bean.PersonsEntity;
import base.bean.RoomEntity;
import base.dao.IPersonDao;
import base.dao.IRoomDao;
import base.service.IPersonService;
import base.service.IRoomService;
import base.utils.ChangeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 30509 on 2018/3/18.
 */
@Controller
@RequestMapping("/test")
public class CheckController {
    @Autowired
    IPersonService iPersonService;
    @RequestMapping("/personsinf")
    private ModelAndView personquery(){
        ModelAndView pq=new ModelAndView();
        List<PersonsEntity> list = iPersonService.findAll();
        pq.addObject("list",list);
        pq.setViewName("personsinf");
        return pq;
    }

    @RequestMapping("/updatepersions")
    private ModelAndView updatepersons(HttpServletRequest request){
        String idd=request.getParameter("id");
        int id= ChangeUtils.parseInt(idd,0);
        String personname=request.getParameter("personname");
        String roomid=request.getParameter("roomid");
        int roomidd= ChangeUtils.parseInt(roomid,-1);
        String cardtype=request.getParameter("cardtype");
        String cardid=request.getParameter("cardid");
        String sex=request.getParameter("sex");
        String phone=request.getParameter("phone");
        iPersonService.updateNameById(personname,roomidd,cardtype,cardid,sex,phone,id);
        return personquery();
    }

    @RequestMapping("/delete")
    private ModelAndView delete(HttpServletRequest request){

        String id=request.getParameter("id");
        int idd= ChangeUtils.parseInt(id,0);
        iPersonService.delete(idd);

        return personquery();
    }

    @Autowired
    IRoomService iRoomService;
    @RequestMapping("/roominf")
    public ModelAndView roomQuery(){
        ModelAndView query=new ModelAndView();
        List<RoomEntity> list = iRoomService.findAll();
        query.addObject("list",list);
        query.setViewName("roominf");
        return query;
    }


    @RequestMapping("/updateperson")
    private ModelAndView updatePerson(HttpServletRequest request){
        ModelAndView update=new ModelAndView();
        String id=request.getParameter("id");
        int idd= ChangeUtils.parseInt(id,-1);
        PersonsEntity qt = iPersonService.findOne(idd);
        update.addObject("qt",qt);
        update.setViewName("updateperson");
        return  update;
    }
}
